<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------


namespace app\shortvideo\model;

use app\BaseModel;
use app\redbag\model\Record;
use app\redbag\model\staffRecord;
use app\reduction\model\Company;
use app\reduction\model\UserRecord;
use think\facade\Db;

class ReductionActive extends BaseModel{

    protected $name = 'longbing_card_reduction_active';

    protected $append=[
        //领取数量
        'get_num',
        //使用数量
        'use_num',
        //公司数量
        'company_num'
    ];

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:领取数量
     */
    public function getGetNumAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'active_id' => $data['id']
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:使用数量
     */
    public function getUseNumAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'active_id' => $data['id'],

                'status'     => 2,
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:41
     * @功能说明:多少公司
     */
    public function getCompanyNumAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['uniacid'])&&isset($data['use_condition'])){

            if($data['use_condition']==1){
                $dis = [

                    'active_id' => $data['id'],

                    'status'    => 1
                ];
                $company = new Company();

                $num = $company->companyCount($dis);
            }else{

                $num = Db::name('longbing_card_company')->where(['uniacid'=>$data['uniacid']])->count();
            }
            return $num;
        }

    }


    /**
     * @author lichuanming
     * @DataTime: 2020-04-21 10:08
     * @功能说明:活动列表
     */
    public function activeList($dis,$page){
        //生成子查询
        $sql = UserRecord::where('active_id','=','`a`.`id`')->fetchSql()->count();
        $data = $this->alias('a')->where("(a.num - ({$sql})) > 0")->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }

}